﻿using System;
using System.Collections.Generic;
using System.Text;

namespace StarBotDotNet.Tournament1.DragoonBattle
{
    public class AttackParty : UnitParty
    {
        Dictionary<StarBotDotNet.UnitType, int> requiredUnits;

        public AttackParty(ProxyBot bot)
        {
            requiredUnits = new Dictionary<UnitType, int>();
            // the battle is going to be a 12v12 battle.

            Utilities.WriteColorLine(ConsoleColor.Blue, bot.GetCurrentPlayerWorkerType().ToString());

            requiredUnits.Add(bot.GetCurrentPlayerWorkerType(), 12);
        }

        internal override void PopulateWithUnits(ProxyBot bot, UnitDictionary currentUnits, List<UnitData> discoveredUnits, List<UnitData> dissapearedUnits, int currentTime)
        {
            Utilities.WriteColorLine(ConsoleColor.DarkCyan, "TimeStamp = {0}", currentTime);
            foreach (KeyValuePair<UnitType, SortedDictionary<int, UnitData>> i in currentUnits)
            {
                //Utilities.WriteColorLine(ConsoleColor.Blue, "PopulateWithUnits : currentUnits = {0}", i.ToString());
                //Utilities.WriteColorLine(ConsoleColor.Blue, "{0}", i.Key.ToString());
                //Utilities.WriteColorLine(ConsoleColor.Blue, "{0}", i.Value.Keys);
                //Utilities.WriteColorLine(ConsoleColor.Blue, "{0}", i.Value.Values);
            }

            foreach (UnitData i in discoveredUnits)
            {
                Utilities.WriteColorLine(ConsoleColor.DarkBlue, "PopulateWithUnits : discoveredUnits = {0}", i.ToString());
            }

            foreach (UnitData i in dissapearedUnits)
            {
                Utilities.WriteColorLine(ConsoleColor.DarkGreen, "PopulateWithUnits : dissapearedUnits = {0}", i.ToString());
            }

            

            base.PopulateWithUnits(bot, currentUnits, discoveredUnits, dissapearedUnits, currentTime);
        }


        List<int> unitHasBeenMoved = new List<int>();

        public override void ExecuteTactics(ProxyBot bot, UnitDictionary currentUnits, int currentTime)
        {
            //foreach (UnitData unit in this.GetAllUnits())
            //{
            //    Utilities.WriteColorLine(ConsoleColor.Blue, "unit.ID = {0}", unit.ID.ToString());
            //    Utilities.WriteColorLine(ConsoleColor.Blue, "unit.OwnedByPlayer = {0}", unit.OwnedByPlayer.ToString());
            //    Utilities.WriteColorLine(ConsoleColor.Blue, "unit.IsIdle = {0}", unit.IsIdle.ToString());
            //    Utilities.WriteColorLine(ConsoleColor.Blue, "unit.IsWorker = {0}", unit.IsWorker.ToString());
            //    Utilities.WriteColorLine(ConsoleColor.Blue, "unit.Type = {0}", unit.Type.ToString());
            //    if (unit.OwnedByPlayer && unit.IsIdle)
            //    {
            //        bot.patrol(unit.Unit.ID, 0, 50);
            //    }
            //}

            // The map size is 64x64
            // Screen - (0,0) corresponds to the top left corner of the screen
            // Map - (0,0) corresponds to the top left corner of the map
            // Mouse - (0,0) corresponds to the tip of the mouse

            foreach (KeyValuePair<UnitType, SortedDictionary<int, UnitData>> i in currentUnits)
            {
                foreach (KeyValuePair<int, UnitData> b in i.Value)
                {
                    //Utilities.WriteColorLine(ConsoleColor.Red, "---------");
                    //Utilities.WriteColorLine(ConsoleColor.White, "Key={0}", b.Key);
                    //Utilities.WriteColorLine(ConsoleColor.White, "Value.ID={0}", b.Value.ID);
                    //Utilities.WriteColorLine(ConsoleColor.White, "Value.IsDead={0}", b.Value.IsDead);
                    //Utilities.WriteColorLine(ConsoleColor.White, "Value.IsIdle={0}", b.Value.IsIdle);
                    //Utilities.WriteColorLine(ConsoleColor.White, "Value.IsWorker={0}", b.Value.IsWorker);
                    //Utilities.WriteColorLine(ConsoleColor.White, "Value.OwnedByPlayer={0}", b.Value.OwnedByPlayer);
                    //Utilities.WriteColorLine(ConsoleColor.White, "Value.Type.ToString()={0}", b.Value.Type.ToString());
                    //Utilities.WriteColorLine(ConsoleColor.White, "Value.Unit.ToString()={0}", b.Value.Unit.ToString());
                    //Utilities.WriteColorLine(ConsoleColor.Red, "---------");

                    if (b.Value.OwnedByPlayer && b.Value.IsIdle && b.Value.Type.Type == Constants.UnitTypes.Protoss_Dragoon)
                    {
                        Utilities.WriteColorLine(ConsoleColor.Green, "bot.patrol({0}, {1}, {2})", b.Value.ID, b.Value.Unit.X + 20, b.Value.Unit.Y + 10);
                       
                        if (unitHasBeenMoved.Contains(b.Value.ID) == false)
                        {
                            //Utilities.WriteColorLine(ConsoleColor.Green, "counterMod =  {0}", counterMod);
                            
                            bot.attackMove(b.Value.ID, b.Value.Unit.X + 40, b.Value.Unit.Y - 1);
                            unitHasBeenMoved.Add(b.Value.ID);
                        }
                    }
                }
                Utilities.WriteColorLine(ConsoleColor.DarkMagenta, "==========");
            }
        }

        public override int Priority
        {
            get { return 0; }
        }

        public override Dictionary<UnitType, int> RequiredUnits
        {
            get { return requiredUnits; }
        }

        protected override bool IsUnused(UnitData data)
        {
            return data.IsIdle;
        }
    }
}
